<template>
	<!-- 通用版本 -->
	<view>
		<!-- 头部搜索框 -->
		<search :placeholder="$t('homepage.WarehouseManage.goodsInstorageBox')" />
		<!-- 查询结果列表 -->
		<zy-empty v-if="cardinfo.length==0"></zy-empty>
		<cardInfo :cardinfo="cardinfo" v-else></cardInfo>
		<bottomBtn fixed :leftText="$t('public.reset')" :rightText="$t('public.submit')"
			:leftDisabled="cardinfo.length>0?false:true" :rightDisabled="cardinfo.length>0?false:true"
			@rightBtn="confirmBtn" @leftBtn="resetBtn">
		</bottomBtn>
		<!-- 储位扫描弹窗 -->
		<zy-popup ref="zyPopup" type='dialog' :title="$t('homepage.WarehouseManage.SPselect')" @cancel="cancel"
			@confirm="submit" closeable :closeOnClickOverlay="false">
			<zy-forms :rules="rules" :model="model" labelWidth="140">
				<zy-form-item :label="$t('public.storage')" required>
					<zy-input v-model="model.storage" :placeholder="$t('public.pleaseInput')+$t('public.storage')"
						@confirm="storageSearch" sufSlot>
						<i class="iconfont icon-saoyisao" @click="scanStorage"></i>
					</zy-input>
				</zy-form-item>
				<zy-form-item :label="$t('public.position')" required>
					<zy-input v-model="model.position" @confirm="positionSearch"
						:placeholder="$t('public.pleaseInput')+$t('public.position')" sufSlot>
						<i class="iconfont icon-saoyisao" @click="scanPosition"></i>
					</zy-input>
				</zy-form-item>
				<zy-form-item :label="$t('public.orderNo')" required>
					<zy-input v-model="model.mono" :placeholder="$t('public.pleaseInput')+$t('public.orderNo')" sufSlot>
						<i class="iconfont icon-saoyisao" @click="scanMono"></i>
					</zy-input>
				</zy-form-item>
				<zy-form-item :label="$t('homepage.WarehouseManage.orderLineNo')" required>
					<zy-input v-model="model.mobodyno"
						:placeholder="$t('public.pleaseInput')+$t('homepage.WarehouseManage.orderLineNo')" sufSlot>
						<i class="iconfont icon-saoyisao" @click="scanMobodyno"></i>
					</zy-input>
				</zy-form-item>
			</zy-forms>
		</zy-popup>
		<!-- 提交二次确认 -->
		<zy-popup ref="popup" :title="$t('homepage.WarehouseManage.goodsInstorageConfirm')" type="dialog"
			@cancel="cancelBtn" @confirm="submitBtn">
			{{$t('public.isSure')}}{{$t('homepage.WarehouseManage.goodsInstorageConfirm')}}{{$t('public.question')}}
		</zy-popup>
	</view>
</template>
<script>
	import {
		getBox,
		storageGetEntityByCode,
		positionGetEntityByCode,
		SaveProductWarehouseIn
	} from "@/api/homepage.js"
	import cardInfo from '../components/infoList.vue';
	export default {
		components: {
			cardInfo,
		},
		data() {
			return {
				cardinfo: [], //卡片数组
				model: {
					position: '', //仓位value值
					storage: '', //储位value值
					mono: '', //订单号value值
					mobodyno: '', //订单行号value值
				},
				rules: {
					position: {
						required: false,
						message: this.$t('public.pleaseInput') + this.$t('public.position'),
						trigger: 'blur'
					},
					storage: {
						required: true,
						message: this.$t('public.pleaseInput') + this.$t('public.storage'),
						trigger: 'blur'
					}
				},
				positionId: '', //仓位id值
				storageId: '', //储位id值
				boxIds: '', //boxIds
			}
		},
		onLoad() {
			this.getscancodedate()
		},
		onUnload() {
			// 移除监听事件      
			uni.$off('scancodedate')
			uni.$off('scancodepopdate')
		},
		methods: {
			//主页红外扫描成品入库箱号事件
			getscancodedate() {
				// 移除监听事件
				uni.$off('scancodepopdate')
				//PDA扫描
				var _this = this
				uni.$on('scancodedate', function(data) {
					_this.searchList(data.code)
				})
			},
			//储位添加弹窗红外扫描事件
			getscancodepopdate() {
				uni.$off('scancodedate')
				//储位红外扫描储位
				var _this = this
				uni.$on('scancodepopdate', function(data) {
					_this.model.storage = data.code.replace(/\s/g, "")
					_this.storageSearch()
				})
			},
			searchList(e) {
				getBox({
					boxNo: e,
					status: 1,
				}).then((res) => {
					console.log(res)
					if (res.code == 0) {
						if (res.data.status == 1) {
							let newGoods = {
								id: res.data.id,
								no: res.data.no,
								num: res.data.num,
								status: res.data.status,
								code: res.data.code,
								boxDetailList: res.data.boxDetailList
							}
							if (this.cardinfo.length == 0) {
								this.cardinfo.push(newGoods)
							} else {
								if (this.cardinfo.findIndex(item => item.code === e) !== -1) {
									uni.showToast({
										title: this.$t('public.searchRepeat'),
										icon: "none",
										duration: 3000
									})
								} else {
									this.cardinfo.push(newGoods)
								}
							}
						}else{
							uni.showToast({
								title: this.$t('homepage.WarehouseManage.unStorageStatus'),
								icon: "none",
								duration: 3000
							})
						}
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((err) => {
					uni.showToast({
						title: err.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//仓位扫描事件scanMono
			scanPosition() {
				var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module")
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_result) {
							this.model.position = ret.resp_result;
							this.positionSearch()
						}
					})
			},
			//储位扫描事件
			scanStorage() {
				var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module")
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_result) {
							this.model.storage = ret.resp_result;
							this.storageSearch()
						}
					})
			},
			//仓位接口
			positionSearch() {
				positionGetEntityByCode({
					code: this.model.position
				}).then((res) => {
					if (res.code == 0) {
						this.positionId = res.data.id
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//储位接口
			storageSearch() {
				storageGetEntityByCode({
					code: this.model.storage
				}).then((res) => {
					if (res.code == 0) {
						this.model.position = res.data.warehousePosition.code
						this.positionSearch()
						this.storageId = res.data.id
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 3000
						})
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//订单号扫描事件
			scanMono() {
				var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module")
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_result) {
							this.model.mono = ret.resp_result;
						}
					})
			},
			//订单行号扫描事件
			scanMobodyno() {
				var mpaasScanModule = uni.requireNativePlugin("Mpaas-Scan-Module")
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_result) {
							this.model.mobodyno = ret.resp_result;
						}
					})
			},
			// 重置
			resetBtn() {
				uni.redirectTo({
					url: "/pages/HomePage/WarehouseManage/ProductManagement/Instorage/index"
				})
			},
			//提交
			confirmBtn() {
				this.$refs.zyPopup.open()
				this.getscancodepopdate()
			},
			//弹窗取消
			cancel() {
				this.getscancodedate()
			},
			// 仓/储选择提交
			submit() {
				if (!this.model.position || !this.model.storage || !this.model.mono || !this.model.mobodyno) {
					uni.showToast({
						title: this.$t('public.pleaseInput'),
						icon: "none",
						duration: 3000
					})
				} else {
					this.$refs.popup.open()
				}
			},
			// 仓/储选择取消
			cancel() {
				this.getscancodedate()
				this.model.position = ''
				this.model.storage = ''
				this.positionId = ''
				this.storageId = ''
				this.model.mono = ''
				this.model.mobodyno = ''
			},
			// 入库
			submitBtn: function() {
				this.boxIds = ''
				for (var i = 0; i < this.cardinfo.length; i++) {
					this.boxIds += this.cardinfo[i].id + ','
				};
				SaveProductWarehouseIn({
					billNo: '', //入库单号
					saleOrderNo: '', //销售单号
					warehouseStorageId: this.storageId, // 储位id
					warehousePositionId: this.positionId, // 仓位id 
					mono: this.model.mono, //订单号
					mobodyno: this.model.mobodyno, //订单行号
					boxIds: this.boxIds, //boxIds
				}).then((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
					if (res.code == 0) {
						uni.redirectTo({
							url: "/pages/HomePage/WarehouseManage/ProductManagement/Instorage/index"
						})
					} else {
						this.$refs.zyPopup.close()
						this.$refs.popup.close()
					}
				}).catch((res) => {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 3000
					})
				});
			},
			//入库调拨提交二次确认取消
			cancelBtn() {
				this.$refs.zyPopup.close()
				this.$refs.popup.close()
			},
		}
	}
</script>
<style lang="scss" scoped>
</style>